﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace WorkHelperTester
{
    [TestClass]
    public class UnitTest1
    {

        const double EarthRadius = 3958.75587;
        double twoMileagePointValue { get; set; }
        [TestMethod]
        public void GPSDistanceNullTestMethod()
        {
            List<pointD> locations = new List<pointD>();
            double mileage = CalculateTotalMileage(locations);
            Assert.IsTrue(mileage == 0);
        }

        [TestMethod]
        public void GPSDistanceOneTestMethod()
        {
            List<pointD> locations = new List<pointD>() 
            { 
                new pointD(40.54835788339363, -111.90300464630127)
            };
            double mileage = CalculateTotalMileage(locations);
            Assert.IsTrue(mileage == 0);
        }

        [TestMethod]
        public void GPSDistanceTwoTestMethod()
        {
            List<pointD> locations = new List<pointD>() 
            { 
                new pointD(40.54835788339363, -111.90300464630127),
                new pointD(40.556265232063254, -111.9106650352478)
            };
            double mileage = CalculateTotalMileage(locations);
            twoMileagePointValue = mileage;
            Assert.IsTrue(mileage != 0);
        }

        [TestMethod]
        public void GPSDistanceMulitpleTestMethod()
        {
            List<pointD> locations = new List<pointD>() 
            { 
                new pointD(40.54835788339363, -111.90300464630127), //to
                new pointD(40.556265232063254, -111.9106650352478), //from
                new pointD(40.54835788339363, -111.90300464630127), //to
                new pointD(40.556265232063254, -111.9106650352478), //from
                new pointD(40.54835788339363, -111.90300464630127), //to
                new pointD(40.556265232063254, -111.9106650352478)  //from
            };
            double mileage = CalculateTotalMileage(locations);
            Assert.IsTrue(mileage > twoMileagePointValue);
        }

        private double CalculateTotalMileage(List<pointD> _locations)
        {
            double TotalMileage = 0;
            if (_locations.Count >= 2)
            {
                int k = 1;
                for (int i = 0; i < _locations.Count - 1; i++)
                {
                    pointD first = _locations[i];
                    pointD second = _locations[k];
                    double dLat = toRadian(second.Longitude - first.Longitude);
                    double dLon = toRadian(second.Latitude - first.Latitude);
                    double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                    Math.Cos(this.toRadian(first.Longitude)) * Math.Cos(this.toRadian(second.Longitude)) *
                        Math.Sin(dLon / 2) * Math.Sin(dLon / 2);

                    double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
                    double d = EarthRadius * c;
                    TotalMileage += d;
                }
            }
            return TotalMileage;
        }
        private double toRadian(double val)
        {
            return (Math.PI / 180) * val;
        }
    }

    class pointD{

        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public pointD(double lat, double lon){
            Latitude = lat;
            Longitude = lon;
        }
    }
}
